<?php
App::import('Controller', 'FutureTweets');
App::import('Model', 'FutureTweet');
App::import('Model', 'User');
App::import('Component', 'Session');
App::import('Component', 'Auth');

class FutureTweetsControllerTest extends ControllerTestCase{
    
    public $fixtures = array('app.future_tweets', 'app.comment');
    public $autoFixtures = false;
    
    var $components = array('Auth', 'Cookie', 'Session', 'RequestHandler');
    
    
    public function startTest(){
        
        $this->FutureTweets = new FutureTweetsControllertest();
        $this->FutureTweets->constructClasses();
	    $this->FutureTweet = new FutureTweet();
        
        $this->FutureTweets->initialize($this->FutureTweets);
        $this->FutureTweetsController = $this->generate('FutureTweets', array(
           'components' => array('Session', 'Auth' => array('User'), 'RequestHandler') //We mock the Auth Component here
               ));
        $this->FutureTweet = ClassRegistry::init('FutureTweet');
        $result = $this->FutureTweet->find('all', array('conditions' => array('id' => 1000)));
        if($result){

        } else {
            $this->FutureTweet->save(array('id' => '1000', 'userID' => 9, 'messageText' => 'test 1'));
        }
    }
    
    public function testapprove(){
        
        $this->FutureTweetsController->Auth->staticExpects($this->any())
			 ->method('User')->with($this->logicalOr(
		 $this->equalTo('userID'), $this->equalTo('accID'), $this->equalTo('admin')))
			 ->will($this->returnCallback(array($this, 'approveCallback')));
        
       
        $this->loadModel('FutureTweet');
        $this->testAction('/FutureTweets/approve/1004');
        $tweet = $this->FutureTweet->find('all', array('conditions' => array('id' => 1004 , 'approved' => 1 )));
        $this->assertEquals('1', $tweet['0']['FutureTweet']['approved']);
    }
    /**
     * Tests the function approveTweet in the FutureTweetsController
     * @author Kareem Yousry
     */

    public function testApproveTweet() {

        $this->FutureTweetsController->Auth->staticExpects($this->any())
        ->method('User', 'User')
        ->with($this->logicalOr(
            $this->equalTo('userID'), $this->equalTo('accID'), $this->equalTo('admin')))
        ->will($this->returnCallback(array($this, 'approveCallback')));

        //disproving Tweet incase it was formerly approved.
        $this->FutureTweet->updateAll(array('FutureTweet.approved' => 'false'),
            array('FutureTweet.id' => '1003'));

        //Testing the action.
        $test = $this->testAction('/futuretweets/approveTweet/1000', array('return' => 'contents'));
        $result = $this->FutureTweet->find('all', array('conditions' => array('id' => 1000), 'fields' => 'approved'));
        $this->assertEquals('1',$result['0']['FutureTweet']['approved']);
    }
    /**
     * Teststhe function delete in the FutureTweetsController
     * @author Kareem Yousry
     */
    public function testDelete() {
        $this->FutureTweetsController->Session->expects($this->once()) -> method('setFlash');
        $this->FutureTweetsController->Auth->staticExpects($this->any())
        ->method('User', 'User')
        ->with($this->logicalOr(
            $this->equalTo('userID'), $this->equalTo('accID'), $this->equalTo('admin')))
        ->will($this->returnCallback(array($this, 'approveCallback')));

        $this->testAction('/futuretweets/delete/1000');
        $result = $this->FutureTweet->find('all', array('conditions' => array('id' => 1000)));
        $this->assertEquals(array(), $result);

    }
    
    public function approveCallback($foo){
        
        if ($foo == 'accID')
            return 1;
        else {
            if ($foo == 'userID') {
                return 1;
            }
        }
        return 0;
    }
	/*
     * @access: public
     * @param: null
     * @Description: This function tests the retweet function in the FutureTweetsController.
     * @author Ahmed El-Fouly
     */
    public function testRetweet() {
        $this->testAction('/FutureTweets/retweet/1/0/4/1');
        $expected = 'RT: First Tweet';
        $result = $this->FutureTweet->find('first', array('conditions' => array('tweetText' => $expected), 'fields' => array('tweetText')));
        $result = $result['FutureTweet']['tweetText'];
        $this->assertEquals($expected, $result);
    }
    /*
     * @access: public
     * 
     * @Description: This function tests the DeleteTweetforJE function in the FutureTweetsController.
     * @author Mark Milad
     */
     public function testdeletetweet() 
        {


        $this->testAction('/futuretweets/DeleteTweetForJE/666');
        $result = $this->FutureTweet->find('all', array('conditions' => array('id' => 666)));
        $this->assertEquals(array(), $result);
       }

        /*
     * @access: public
     * 
     * @Description: This function tests the save1 function in the FutureTweetsController.
     * @author Youssef Madkour
     */

        public function testsave1(){
             $this->loadModel('FutureTweet');
        $this->tesstAction('/FutureTweets/save1/666');
        $expected='test1';

        $result = $this->FutureTweet->find('first', array('conditions' => array('tweetText' => $expected)));

         $this->assertEquals(0, $result['FutureTweet']['saved']);




       }

        /*
     * @access: public
     * 
     * @Description: This function tests the editModerator function in the FutureTweetsController.
     * @author Youssef Madkour
     */

       public function testeditModerator(){

         $this->FutureTweetsController->Auth->staticExpects($this->any())
             ->method('User')->with($this->logicalOr(
         $this->equalTo('userID'), $this->equalTo('accID'), $this->equalTo('admin')))
             ->will($this->returnCallback(array($this, 'approveCallback')));

        $this->loadModel('FutureTweet');
        $this->tesstAction('/FutureTweets/editModerator/666');

        $expected='test1';
        $expectedTime = '2012-05-25 08:13:28';
        $result = $this->FutureTweet->find('first', array('conditions' => array('id' => '666')));
        $this->assertEquals(NULL, $result);

          $result = $this->FutureTweet->find('first', array('conditions' => array('tweetText' => $expected)));
          $this->assertEquals($expected, $result['FutureTweet']['tweetText']);

          $result = $this->FutureTweet->find('first', array('conditions' => array('release_date'=>$expectedTime)));
          $this->assertEquals($expectedTime, $result['FutureTweet']['release_date']);



       }
}